<template>
  <div class="person">
    姓：<input type="text" v-model="firstName"><br>
    名：<input type="text" v-model="lastName"><br>
    全名：<span>{{ fullName }}</span>
    <br>
    全名：<span>{{ fullName }}</span>
    <br>
    全名：<span>{{ fullName }}</span>
    <br>
    <button @click="changeName">修改名称</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { computed, ref } from 'vue'

let firstName = ref('zhang');
let lastName = ref('san');

//计算属性，且是只读的
// let fullName = computed(() => {
//   console.log(1);
//   return firstName.value.slice(0, 1).toUpperCase() + firstName.value.slice(1) + lastName.value;
// })

//计算属性，既读取又修改
let fullName = computed({
  get() {
    console.log(1);
    return firstName.value.slice(0, 1).toUpperCase() + firstName.value.slice(1) + lastName.value;
  },
  set(val) {
    console.log(val)
    firstName.value = val.split("-")[0];
    lastName.value = val.split("-")[1];
  }
});
function changeName() {
  fullName.value = 'li-si';
}
</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}

button {
  margin: 0 5px;
}
</style>